﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using System.Drawing;
public partial class ExpertManager_EditExpert : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        IDataReader datar= null;
        Session["name"] = Tser.Text;
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("UserName");
        dt.Columns.Add("Family");
        dt.Columns.Add("Address");
        dt.Columns.Add("Telephone");
        dt.Columns.Add("ProvinceName");
        dt.Columns.Add("EmailAddress");
        dt.Columns.Add("Mobile");
        dt.Columns.Add("ProvinceId");
        dt.Columns.Add("ID");
             if(RadioButtonList1.SelectedValue =="")
            {

                LBLStatus.Text = "لطفا روش جستجو را انتخاب کنید";
            }

             else if (RadioButtonList1.SelectedItem.Value != "")
            {

               LBLStatus.Visible = false;
               Tser.Enabled = true;
               
               if(RadioButtonList1.SelectedItem.Value=="Name")
                datar=ExpProvince.FetchByParameter(ExpProvince.Columns.Name, SubSonic.Comparison.Like,"%"+Tser.Text+"%");
               else if (RadioButtonList1.SelectedItem.Value == "LastName")
                 datar = ExpProvince.FetchByParameter(ExpProvince.Columns.Family, SubSonic.Comparison.Like,"%"+Tser.Text+"%");
                 else if (RadioButtonList1.SelectedItem.Value == "Mobile")
                   datar = ExpProvince.FetchByParameter(ExpProvince.Columns.Mobile, SubSonic.Comparison.Like,"%"+Tser.Text+"%");
                  else if (RadioButtonList1.SelectedItem.Value == "NatioCode")
                   datar = ExpProvince.FetchByParameter(ExpProvince.Columns.NationalCode, SubSonic.Comparison.Like,"%"+Tser.Text+"%");
                   while(datar.Read()){
                   ProfileCommon p = Profile.GetProfile(datar["UserName"].ToString());
                   if (p.Common.Status == "On")
                   {
                       DataRow dr;
                       dr = dt.NewRow();
                       dr["Name"] = datar["Name"].ToString();
                       dr["UserName"] = datar["UserName"].ToString();
                       dr["Family"] = datar["Family"].ToString();
                       dr["Address"] = datar["Address"].ToString();
                       dr["Telephone"] = datar["Telephone"].ToString();
                       dr["ProvinceName"] = datar["ProvinceName"].ToString();
                       dr["ProvinceId"] = datar["ProvinceId"].ToString();
                       dr["EmailAddress"] = datar["EmailAddress"].ToString();
                       dr["Mobile"] = datar["Mobile"].ToString();
                       dr["ID"] = datar["ID"].ToString();
                       dt.Rows.Add(dr);
                   }
                   }
               datar.Close();
               GrdExpert.DataSource = dt;
               GrdExpert.DataBind();
               pnlShoData.Visible = true;
 
               }

           
    }
    protected void GrdExpert_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        
        int index = Convert.ToInt32(e.CommandArgument);
        int ExpertId=Convert.ToInt32(GrdExpert.Rows[index].Cells[6].Text);
        string username = (GrdExpert.Rows[index].Cells[9].Text).ToString(); ;
        Session["UserName"] = username;
        Session["ExpertId"] = ExpertId;
        switch (e.CommandName) { 
        
            case "Edit":
               PnlEditExpert.Visible = true;
               PnlResetPass.Visible = false;
               pnlShoData.Visible = false;
               Tser.Enabled = false;
               Button1.Enabled = false;
               IDataReader dr = ExpProvince.FetchByParameter(ExpProvince.Columns.Id, SubSonic.Comparison.Equals, ExpertId);
               
               if (dr.Read())
               {
                   Txname.Text = dr["Name"].ToString();
                   TxFAmily.Text = dr["Family"].ToString();
                   Txaddre.Text = dr["Address"].ToString();
                   Txemail.Text = dr["EmailAddress"].ToString();
                   TxMobil.Text = dr["Mobile"].ToString();
                   Txtele.Text = dr["Telephone"].ToString();
                   TxNatio.Text=dr["NationalCode"].ToString();
                   Drpro.Text= dr["ProvinceId"].ToString();
                   Session["Province"]= dr["ProvinceId"].ToString();
                   dr.Close();
                 
               }
                          
                break;
            case "RstPass": {
                PnlResetPass.Visible = true;
                Tser.Enabled = false;
                Button1.Enabled = false;
                PnlEditExpert.Visible = false;
                TxUserName.Text = Session["UserName"].ToString();
                break;
            }
            case "Del": {

                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "confirm('You are overriding the existing pay grade. Do you want to Override ?');", true);
                    PnlEditExpert.Visible = false;
                    PnlResetPass.Visible = false;
                    MembershipUser User = Membership.GetUser(username);
                    ProfileCommon p1 = Profile.GetProfile(Session["UserName"].ToString());
                    p1.Common.Status = "Off";
                    User.IsApproved = false;
                    Membership.UpdateUser(User);
                    p1.Save();
                    Lblsta1.Text = "کاربر غیر فعال گردید";
                    GrdExpert.Rows[index].BackColor = Color.Gray;
                    break;
                
            }

       
        }

    }

  
    protected void GrdExpert_RowEditing(object sender, GridViewEditEventArgs e)
    {
       
        GrdExpert.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        GrdExpert.DataBind();
    

    }
    protected void BtnEdit_Click(object sender, EventArgs e)
    {
        //MembershipUser user = Membership.GetUser(Session["UserName"].ToString());
        //string b = user.UserName;
        ProfileCommon p = Profile.GetProfile(Session["UserName"].ToString());
        int ProvinveId =Convert.ToInt32( Drpro.SelectedItem.Value);
        SubSonic.Query sq = TblExpert.CreateQuery();
        sq.QueryType = SubSonic.QueryType.Update;
        sq.AddUpdateSetting(TblExpert.Columns.ProvinceId, ProvinveId);
        sq.AddUpdateSetting(TblExpert.Columns.Name, Txname.Text);
        sq.AddUpdateSetting(TblExpert.Columns.Family, TxFAmily.Text);
        sq.AddUpdateSetting(TblExpert.Columns.Address, Txaddre.Text);
        sq.AddUpdateSetting(TblExpert.Columns.Mobile, TxMobil.Text);
        sq.AddUpdateSetting(TblExpert.Columns.Telephone, Txtele.Text);
        sq.AddUpdateSetting(TblExpert.Columns.NationalCode, TxNatio.Text);
        sq.AddUpdateSetting(TblExpert.Columns.EmailAddress, Txemail.Text);
        sq.WHERE(TblExpert.Columns.Id, SubSonic.Comparison.Equals, Session["ExpertId"]);
        p.Common.FirstName = Txname.Text;
        p.Common.LastName = TxFAmily.Text;
        p.Common.Address = Txaddre.Text;
        p.Common.Mobile = TxMobil.Text;
        p.Common.Email = Txemail.Text;
        p.Common.Phone = Txtele.Text;
        p.Save();
        sq.Execute();
        LblSuccess.Text = "ویرایش با موفقیت انجام شد";
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Tser.Enabled = true;
    }
   
    protected void BtnChgPass_Click(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser(Session["UserName"].ToString());
        string Oldpassword = user.ResetPassword();
        bool result = user.ChangePassword(Oldpassword, Txpass.Text);
        lblsuccess2.Text = "تغییر رمز با موفقیت انجام شد";
    }
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~\ExpertManager\Home.aspx");
    }
}